<?php

namespace app\controller\admin\sgz;

use app\model\admin\sgz\SgzBattleTeam;
use app\model\admin\sgz\SgzGenerals;
use app\model\admin\sgz\SgzTeamGenerals;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Career
 * @package app\controller\admin\ah
 */
class Generals extends Backend
{
    protected $model;
    public $orderRule=['id'=>'DESC'];
    public function initialize()
    {
        $this->model = new SgzGenerals();
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    //设置展示
    public function setIsShow()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['is_show'] = $fieldVal;

        if($fieldVal == 2){
            //查询阵容存不存在
            $count = (new SgzTeamGenerals())->hasWhere('team')->where("generals_id",$id)->count();
            if($count>0){
                return $this->error('武将存在阵容');
            }
            $count = (new SgzBattleTeam())
                ->hasWhere("battle")
                ->where(function ($query)use($id){
                    return $query ->where("master_generals_id",$id)
                        ->whereOr("one_generals_id",$id)
                        ->whereOr("two_generals_id",$id);
                })
                ->count();
            if($count>0){
                return $this->error('武将存在对战');
            }
        }
        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //删除
    public function del()
    {
        $ids = array_filter($this->request->post('ids'));
        if (!$ids) {
            return $this->error('参数ids不能为空');
        }
        try{
            foreach ($ids as $id){
                //查询阵容存不存在
                $count = (new SgzTeamGenerals())->hasWhere('team')->where("generals_id",$id)->count();
                if($count>0){
                    return $this->error('武将存在阵容');
                }
                $count = (new SgzBattleTeam())
                    ->hasWhere("battle")
                    ->where(function ($query)use($id){
                        return $query ->where("master_generals_id",$id)
                            ->whereOr("one_generals_id",$id)
                            ->whereOr("two_generals_id",$id);
                    })
                    ->count();
                if($count>0){
                    return $this->error('武将存在对战');
                }
            }
            if ($this->model->destroy($ids)) {
                $this->clearCache();
                return $this->success('数据删除成功');
            } else {
                return $this->error('数据删除失败');
            }
        }catch (\Exception $e){
            return $this->exceptionError($e);
        }
    }

}